// 依赖
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/vuex/store.js'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import baseHttp from '@/assets/js/base_http.js'
import Lockr from 'lockr'
import config from '@/assets/js/config.js'
import _g from '@/assets/js/global.js'
// 组件
let Login = (resolve) => require(['@/components/Account/Login.vue'], resolve)
let refresh = (resolve) => require(['@/components/refresh.vue'], resolve)
let Home = (resolve) => require(['@/components/Home.vue'], resolve)
let App = (resolve) => require(['@/components/App.vue'], resolve)

let systemConfig = (resolve) => require(['@/components/Admin/admin/config/add.vue'], resolve)

// 菜单权限类(前台/后台)
let menuList = (resolve) => require(['@/components/Admin/admin/menu/list.vue'], resolve)
let menuEdit = (resolve) => require(['@/components/Admin/admin/menu/edit.vue'], resolve)

let memberMenuList = (resolve) => require(['@/components/Index/menu/list.vue'], resolve)
let memberMenuEdit = (resolve) => require(['@/components/Index/menu/edit.vue'], resolve)

let ruleList = (resolve) => require(['@/components/Admin/admin/rule/list.vue'], resolve)
let ruleEdit = (resolve) => require(['@/components/Admin/admin/rule/edit.vue'], resolve)

let memberRuleList = (resolve) => require(['@/components/Index/rule/list.vue'], resolve)
let memberRuleEdit = (resolve) => require(['@/components/Index/rule/edit.vue'], resolve)

let groupList = (resolve) => require(['@/components/Admin/admin/group/list.vue'], resolve)
let groupEdit = (resolve) => require(['@/components/Admin/admin/group/edit.vue'], resolve)

let memberGroupList = (resolve) => require(['@/components/Index/group/list.vue'], resolve)
let memberGroupEdit = (resolve) => require(['@/components/Index/group/edit.vue'], resolve)

// 部门岗位
let positionList = (resolve) => require(['@/components/Admin/admin/position/list.vue'], resolve)
let positionEdit = (resolve) => require(['@/components/Admin/admin/position/edit.vue'], resolve)

let structureList = (resolve) => require(['@/components/Admin/admin/structure/list.vue'], resolve)
let structureEdit = (resolve) => require(['@/components/Admin/admin/structure/edit.vue'], resolve)

let adminList = (resolve) => require(['@/components/Admin/admin/user/list.vue'], resolve)
let adminEdit = (resolve) => require(['@/components/Admin/admin/user/edit.vue'], resolve)

let articleList = (resolve) => require(['@/components/Index/article/list.vue'], resolve)
let articleEdit = (resolve) => require(['@/components/Index/article/edit.vue'], resolve)

let categoryList = (resolve) => require(['@/components/Index/category/list.vue'], resolve)
let categoryEdit = (resolve) => require(['@/components/Index/category/edit.vue'], resolve)

let planList = (resolve) => require(['@/components/Index/plan/list.vue'], resolve)
let planEdit = (resolve) => require(['@/components/Index/plan/edit.vue'], resolve)

let mailList = (resolve) => require(['@/components/Busi/mail/mail/list.vue'], resolve)
let mailAdd = (resolve) => require(['@/components/Busi/mail/mail/add.vue'], resolve)
let mailEdit = (resolve) => require(['@/components/Busi/mail/mail/edit.vue'], resolve)

let memberList = (resolve) => require(['@/components/Index/member/list.vue'], resolve)
let memberEdit = (resolve) => require(['@/components/Index/member/edit.vue'], resolve)

let Test = (resolve) => require(['@/components/Index/test.vue'], resolve)

Vue.use(VueRouter)

/**
 * meta参数解析
 * hideLeft: 是否隐藏左侧菜单，单页菜单为true
 * module: 菜单所属模块
 * menu: 所属菜单，用于判断三级菜单是否显示高亮，如菜单列表、添加菜单、编辑菜单都是'menu'，用户列表、添加用户、编辑用户都是'user'，如此类推
 */

const routes = [
  { path: '/', component: Login, name: 'Login' },
  {
    path: '/admin',
    component: Home,
    children: [
      { path: '/refresh', component: refresh, name: 'refresh' }
    ]
  },
  {
    path: '/front',
    component: App,
    children: [
      { path: 'test', component: Test, name: 'test' }
    ]
  },
  {
    path: '/admin',
    component: Home,
    children: [
      { path: 'config/add', component: systemConfig, name: 'systemConfig', meta: { hideLeft: false, module: 'Admin', menu: 'systemConfig' }}
    ]
  },
  {
    path: '/admin',
    component: Home,
    children: [
      { path: 'menu/list', component: menuList, name: 'menuList', meta: { hideLeft: false, module: 'Admin', menu: 'menu' }},
      { path: 'menu/edit/', component: menuEdit, name: 'menuEdit', meta: { hideLeft: false, module: 'Admin', menu: 'menu' }},
      { path: 'menu', component: menuList, name: 'menuList', meta: { hideLeft: false, module: 'Admin', menu: 'menu' }}
    ]
  },
  {
    path: '/admin',
    component: Home,
    children: [
      { path: 'rule/list', component: ruleList, name: 'ruleList', meta: { hideLeft: false, module: 'Admin', menu: 'rule' }},
      { path: 'rule/edit/:id', component: ruleEdit, name: 'ruleEdit', meta: { hideLeft: false, module: 'Admin', menu: 'rule' }},
      { path: 'rule/edit/', component: ruleEdit, name: 'ruleEdit', meta: { hideLeft: false, module: 'Admin', menu: 'rule' }},
      { path: 'rule', component: ruleList, name: 'ruleList', meta: { hideLeft: false, module: 'Admin', menu: 'rule' }}
    ]
  },
  {
    path: '/admin',
    component: Home,
    children: [
      { path: 'group/list', component: groupList, name: 'groupList', meta: { hideLeft: false, module: 'Admin', menu: 'group' }},
      { path: 'group/edit/:id', component: groupEdit, name: 'groupEdit', meta: { hideLeft: false, module: 'Admin', menu: 'group' }},
      { path: 'group/edit/', component: groupEdit, name: 'groupEdit', meta: { hideLeft: false, module: 'Admin', menu: 'group' }},
      { path: 'group', component: groupList, name: 'groupList', meta: { hideLeft: false, module: 'Admin', menu: 'group' }}
    ]
  },
  {
    path: '/admin/index/',
    component: Home,
    children: [
      { path: 'memberMenu/list', component: memberMenuList, name: 'memberMenuList', meta: { hideLeft: false, module: 'Admin', menu: 'memberMenu' }},
      { path: 'memberMenu/edit/', component: memberMenuEdit, name: 'memberMenuEdit', meta: { hideLeft: false, module: 'Admin', menu: 'memberMenu' }},
      { path: 'memberMenu', component: memberMenuList, name: 'memberMenuList', meta: { hideLeft: false, module: 'Admin', menu: 'menu' }}
    ]
  },
  {
    path: '/admin/index/',
    component: Home,
    children: [
      { path: 'memberRule/list', component: memberRuleList, name: 'memberRuleList', meta: { hideLeft: false, module: 'Admin', menu: 'memberRule' }},
      { path: 'memberRule/edit/:id', component: memberRuleEdit, name: 'memberRuleEdit', meta: { hideLeft: false, module: 'Admin', menu: 'memberRule' }},
      { path: 'memberRule/edit/', component: memberRuleEdit, name: 'memberRuleEdit', meta: { hideLeft: false, module: 'Admin', menu: 'memberRule' }},
      { path: 'memberRule', component: memberRuleList, name: 'memberRuleList', meta: { hideLeft: false, module: 'Admin', menu: 'memberRule' }}
    ]
  },
  {
    path: '/admin/index/',
    component: Home,
    children: [
      { path: 'memberGroup/list', component: memberGroupList, name: 'memberGroupList', meta: { hideLeft: false, module: 'Admin', menu: 'memberGroup' }},
      { path: 'memberGroup/edit/:id', component: memberGroupEdit, name: 'memberGroupEdit', meta: { hideLeft: false, module: 'Admin', menu: 'memberGroup' }},
      { path: 'memberGroup/edit/', component: memberGroupEdit, name: 'memberGroupEdit', meta: { hideLeft: false, module: 'Admin', menu: 'memberGroup' }},
      { path: 'memberGroup', component: memberGroupList, name: 'memberGroupList', meta: { hideLeft: false, module: 'Admin', menu: 'memberGroup' }}
    ]
  },
  {
    path: '/admin',
    component: Home,
    children: [
      { path: 'structure/list', component: structureList, name: 'structureList', meta: { hideLeft: false, module: 'Admin', menu: 'structure' }},
      { path: 'structure/edit/:id', component: structureEdit, name: 'structureEdit', meta: { hideLeft: false, module: 'Admin', menu: 'structure' }},
      { path: 'structure/edit/', component: structureEdit, name: 'structureEdit', meta: { hideLeft: false, module: 'Admin', menu: 'structure' }},
      { path: 'structure', component: structureList, name: 'structureList', meta: { hideLeft: false, module: 'Admin', menu: 'structure' }}
    ]
  },
  {
    path: '/admin',
    component: Home,
    children: [
      { path: 'position/list', component: positionList, name: 'positionList', meta: { hideLeft: false, module: 'Admin', menu: 'position' }},
      { path: 'position/edit/:id', component: positionEdit, name: 'positionEdit', meta: { hideLeft: false, module: 'Admin', menu: 'position' }},
      { path: 'position/edit/', component: positionEdit, name: 'positionEdit', meta: { hideLeft: false, module: 'Admin', menu: 'position' }},
      { path: 'position', component: positionList, name: 'positionList', meta: { hideLeft: false, module: 'Admin', menu: 'position' }}
    ]
  },
  {
    path: '/admin',
    component: Home,
    children: [
      { path: 'user/list', component: adminList, name: 'adminList', meta: { hideLeft: false, module: 'Admin', menu: 'user' }},
      { path: 'user/edit/:id', component: adminEdit, name: 'adminEdit', meta: { hideLeft: false, module: 'Admin', menu: 'user' }},
      { path: 'user/edit/', component: adminEdit, name: 'adminEdit', meta: { hideLeft: false, module: 'Admin', menu: 'user' }},
      { path: 'user', component: adminList, name: 'adminList', meta: { hideLeft: false, module: 'Admin', menu: 'user' }}
    ]
  },
  {
    path: '/busi',
    component: Home,
    children: [
      { path: 'mail/list', component: mailList, name: 'mailList', meta: { hideLeft: false, module: 'Busi', menu: 'mail' }},
      { path: 'mail/add', component: mailAdd, name: 'mailAdd', meta: { hideLeft: false, module: 'Busi', menu: 'mail' }},
      { path: 'mail/edit/:id', component: mailEdit, name: 'mailEdit', meta: { hideLeft: false, module: 'Busi', menu: 'mail' }},
      { path: 'mail', component: mailList, name: 'mailList', meta: { hideLeft: false, module: 'Busi', menu: 'mail' }}
    ]
  },
  // 前台管理
  {
    path: '/admin/index/',
    component: Home,
    children: [
      { path: 'article/list', component: articleList, name: 'articleList', meta: { hideLeft: false, module: 'Admin', menu: 'article' }},
      { path: 'article/edit/:id', component: articleEdit, name: 'articleEdit', meta: { hideLeft: false, module: 'Admin', menu: 'article' }},
      { path: 'article/edit/', component: articleEdit, name: 'articleEdit', meta: { hideLeft: false, module: 'Admin', menu: 'article' }},
      { path: 'article', component: articleList, name: 'articleList', meta: { hideLeft: false, module: 'Admin', menu: 'article' }}
    ]
  },
  {
    path: '/admin/index/',
    component: Home,
    children: [
      { path: 'category/list', component: categoryList, name: 'categoryList', meta: { hideLeft: false, module: 'Admin', menu: 'category' }},
      { path: 'category/edit/:id', component: categoryEdit, name: 'categoryEdit', meta: { hideLeft: false, module: 'Admin', menu: 'category' }},
      { path: 'category/edit', component: categoryEdit, name: 'categoryEdit', meta: { hideLeft: false, module: 'Admin', menu: 'category' }},
      { path: 'category', component: categoryList, name: 'categoryList', meta: { hideLeft: false, module: 'Admin', menu: 'category' }}
    ]
  },
  {
    path: '/admin/index/',
    component: Home,
    children: [
      { path: 'member/list', component: memberList, name: 'memberList', meta: { hideLeft: false, module: 'Admin', menu: 'member' }},
      { path: 'member/edit/:id', component: memberEdit, name: 'memberEdit', meta: { hideLeft: false, module: 'Admin', menu: 'member' }},
      { path: 'member/edit/', component: memberEdit, name: 'memberEdit', meta: { hideLeft: false, module: 'Admin', menu: 'member' }},
      { path: 'member', component: memberList, name: 'memberList', meta: { hideLeft: false, module: 'Admin', menu: 'member' }}
    ]
  },
  {
    path: '/admin/index/',
    component: Home,
    children: [
      { path: 'plan/list', component: planList, name: 'planList', meta: { hideLeft: false, module: 'Admin', menu: 'plan' }},
      { path: 'plan/edit/:id', component: planEdit, name: 'planEdit', meta: { hideLeft: false, module: 'Admin', menu: 'plan' }},
      { path: 'plan/edit/', component: planEdit, name: 'planEdit', meta: { hideLeft: false, module: 'Admin', menu: 'plan' }},
      { path: 'plan', component: planList, name: 'planList', meta: { hideLeft: false, module: 'Admin', menu: 'plan' }}
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: __dirname,
  routes
})
router.beforeEach(async(to, from, next) => {
  const hideLeft = to.meta.hideLeft
  store.dispatch('showLeftMenu', hideLeft)
  // 如果跳转去登录页
  if (to.name !== 'Login') {
    const expire = Lockr.get('expire')
    const advanceTime = config.advanceTime
    const nowTime = Math.floor(new Date().getTime() / 1000)
    const infos = baseHttp.apiPost('admin/admin/detail')
    const quees = [infos]
    // if (nowTime >= (expire - advanceTime)) {
    const refresh = baseHttp.apiPost('admin/admin/refresh') // 获取新token
    quees.push(refresh)
    // }
    const result = await Promise.all(quees)
    // 如果请求多于1个（获取用户信息）
    if (result.length >= 1) {
      if ((result[0].code === 200) || (result[0].code === 0)) {
        const data = result[0].data
        store.dispatch('setMenus', data.menusList)      // 菜单信息
        store.dispatch('setRules', data.authList)       // 权限信息
        store.dispatch('setUser', data.userInfo)       // 用户信息
      } else {
        _g.toastMsg('warning', '请重新登录')
        setTimeout(() => {
          router.replace('/')
        }, 1500)
        return
      }
    };
    // 如果请求多于2个(获取用户信息，刷新token)
    if (result.length >= 2) {
      if ((result[0].code === 200) || (result[0].code === 0)) {
        const data = result[1].data
        Lockr.set('admin-token', data.token)              // 权限认证
        Lockr.set('expire', data.expire)              // 权限认证
      } else {
        _g.toastMsg('warning', '请重新登录')
        setTimeout(() => {
          router.replace('/')
        }, 1500)
        return
      }
    }
  }
  NProgress.start()
  next()
})

router.afterEach(transition => {
  NProgress.done()
})

export default router

